package com.wyvoid.forum.service.impl;

import com.wyvoid.core.persistent.Pagination;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import com.wyvoid.forum.dao.IForumDAO;
import com.wyvoid.forum.dao.IPostDAO;
import com.wyvoid.forum.dao.ITopicDAO;
import com.wyvoid.forum.domain.Forum;
import com.wyvoid.forum.domain.Post;
import com.wyvoid.forum.domain.Topic;
import com.wyvoid.forum.service.IPostService;
import com.wyvoid.forum.util.ForumUtil;
import com.wyvoid.forum.web.search.SearchBean;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-16
 * Time: 22:26:15
 */
@Transactional
public class PostService implements IPostService {
    private IPostDAO postDAO;
    private IForumDAO forumDAO;
    private ITopicDAO topicDAO;

    public void setPostDAO(IPostDAO postDAO) {
        this.postDAO = postDAO;
    }

    public void setForumDAO(IForumDAO forumDAO) {
        this.forumDAO = forumDAO;
    }

    public void setTopicDAO(ITopicDAO topicDAO) {
        this.topicDAO = topicDAO;
    }

    public void deletePosts(int topicId) {
        postDAO.deletePosts(topicId);
    }

    public HibernatePage<Post> getPosts(final long topicId, final Pagination pagination) {
        return postDAO.getPosts(topicId, pagination);
    }

    public long countPost() {
        return postDAO.countPost();
    }

    public void newTopic(Topic topic, Post post) {
        topicDAO.create(topic);
        post.setTopicId(topic.getId());
        postDAO.create(post);
        topic.setLastPost(post);
        topic.setPostCount(1);
        Forum forum = forumDAO.getById(topic.getForumId());
        forum.setTopicCount(forum.getTopicCount() + 1);
        forum.setPostCount(forum.getPostCount() + 1);
        incrementTodayPostCount(forum);
        forum.setLastPost(post);
    }

    public void replyTopic(Post post) {
        postDAO.create(post);
        Topic topic = topicDAO.getById(post.getTopicId());
        topic.setLastPost(post);
        topic.setPostCount(topic.getPostCount() + 1);
        Forum forum = forumDAO.getById(topic.getForumId());
        forum.setPostCount(forum.getPostCount() + 1);
        incrementTodayPostCount(forum);
        forum.setLastPost(post);
    }

    public void incrementTodayPostCount(Forum forum) {
        Post post=forum.getLastPost();
        Date date=new Date();
        if(post!=null&& DateUtils.isSameDay(date,post.getCreateTime())){
            forum.setPostCountToday(forum.getPostCountToday()+1);
        }else{
            forum.setPostCountToday(1);
        }

        String lastPostDateStr= (String) ForumUtil.settings.get("lastPostDate");
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date lastPostDate=sdf.parse(lastPostDateStr);
            if(DateUtils.isSameDay(lastPostDate,date)){
                int postCountToday=Integer.parseInt((String) ForumUtil.settings.get("postCountToday"))+1;
                ForumUtil.putSettingsAndToDB("postCountToday",postCountToday+"");
            }else{
                String dateStr=sdf.format(date);
                ForumUtil.putSettingsAndToDB("lastPostDate",dateStr);
                ForumUtil.putSettingsAndToDB("postCountToday","1");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public Post getPost(long postId) {
        return postDAO.getById(postId);
    }

    public void updatePost(Post tempPost) {
        postDAO.update(tempPost);
    }

    public HibernatePage<Post> searchPosts(SearchBean searchBean, Pagination pagination) {
        return postDAO.searchPosts(searchBean,pagination);
    }
}
